package junit.admin;

import com.jfinal.aop.Inject;
import io.jboot.test.MockMvcResult;
import io.jpress.model.Department;
import io.jpress.service.DepartmentService;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class AdminDepartmentTester extends AdminBaseTester {


    @Inject
    private DepartmentService departmentService;

    @Test
    public void startDepartmentTestCases() {

        //用户登录，默认账号 admin 123456
        doLogin();

        //新增部门
        testAddDepartment();

        //更新部门信息
        testUpdateDepartment();

        //删除部门信息
        testDeleteDepartment();

    }


    /**
     * 新增部门
     */
    public void testAddDepartment() {
        Department department = new Department();

        department.setPid(0L);

        //名称边界测试
        Map<String, Object> paras = buildParas(department, "department");
        MockMvcResult mvcResult = postWithCsrfToken("/admin/department/doSave", paras);
        Assert.assertTrue(mvcResult.getContent().contains("名称不能为空"));


        department.setName("department");
        paras = buildParas(department, "department");
        mvcResult = postWithCsrfToken("/admin/department/doSave", paras);

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long departmentId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("departmentId", departmentId);

        mvcResult.printResult();
    }


    /**
     * 测试更新部门信息
     */
    public void testUpdateDepartment() {

        Department article = new Department();
        article.setName("test name");
        article.setId((Long) getContainerData("departmentId"));

        Map<String, Object> paras = buildParas(article, "department");

        MockMvcResult mvcResult = postWithCsrfToken("/admin/department/doSave", paras);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
        Assert.assertTrue(mvcResult.getContentAsJSONObject().get("state").equals("ok"));

        Long departmentId = mvcResult.getContentAsJSONObject().getLong("id");
        putContainerData("departmentId", departmentId);

        final Department byId = departmentService.findById(departmentId);
        Assert.assertEquals(byId.getName(), article.getName());
    }


    /**
     * 测试删除部门
     */
    public void testDeleteDepartment() {
        Long articleId = (Long) getContainerData("departmentId");
        MockMvcResult mvcResult = getWithCsrfToken("/admin/department/doDel/" + articleId);
        mvcResult.printResult();

        Assert.assertTrue(mvcResult.getStatus() == 200);
    }
}

